/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Option.java
 *
 * Created on May 17, 2012, 4:03:09 PM
 */
package db.server.GUI;

import db.server.BUS.DBServerBUS;
import db.server.BUS.OptionBUS;
import db.server.DTO.ConfigDTO;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.xml.parsers.ParserConfigurationException;

/**
 *
 * @author QuangKhai
 */
public final class Option extends javax.swing.JPanel {
    private ConfigDTO config = new ConfigDTO();
    private boolean usernameClick = false;
    private boolean passworkClick = false;
    private boolean workspaceClick = false;
    private DBServerBUS serverBUS = new DBServerBUS();
    private OptionBUS optionBUS = new OptionBUS();
    private String path;
    private JTabbedPane tabpane;
    
    /** Creates new form Option */
    public Option(ConfigDTO config, String path, JTabbedPane tabpane) {
        initComponents();
        this.config = config;
        this.path = path;
        this.tabpane = tabpane;
        
        showData();
    }

    ///Show config file
    public void showData(){
        tf_username.setText(config.getUsername());
        tf_password.setText(config.getPassword());
        tf_workspace.setText(config.getWorkspace());
    }
    
    ///Processing change workspace
    public void changeWorkSpace(String oldPath, String newPath){        
        File src = new File(oldPath);
        File dst = new File(newPath);

        if(!dst.exists()){
            dst.mkdir();
        }
        
        try {
            serverBUS.copy(src, dst);
        } catch (IOException ex) {
            Logger.getLogger(DBServer.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        serverBUS.Delete(src);                   
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton3 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        tf_username = new javax.swing.JTextField();
        bt_username = new javax.swing.JButton();
        tf_password = new javax.swing.JPasswordField();
        tf_retypepassword = new javax.swing.JPasswordField();
        bt_editpass = new javax.swing.JButton();
        tf_workspace = new javax.swing.JTextField();
        bt_editworkspace = new javax.swing.JButton();
        bt_ok = new javax.swing.JButton();
        bt_cancel = new javax.swing.JButton();
        bt_apply = new javax.swing.JButton();

        jButton3.setText("jButton3");

        jLabel1.setText("Username");

        jLabel2.setText("Password");

        jLabel3.setText("Retype Password");

        jLabel4.setText("Workspace");

        tf_username.setEnabled(false);

        bt_username.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/cog_edit.png"))); // NOI18N
        bt_username.setToolTipText("Change username");
        bt_username.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_usernameActionPerformed(evt);
            }
        });

        tf_password.setEnabled(false);

        tf_retypepassword.setEnabled(false);

        bt_editpass.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/cog_edit.png"))); // NOI18N
        bt_editpass.setToolTipText("Change password");
        bt_editpass.setVerifyInputWhenFocusTarget(false);
        bt_editpass.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_editpassActionPerformed(evt);
            }
        });

        tf_workspace.setEnabled(false);

        bt_editworkspace.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/cog_edit.png"))); // NOI18N
        bt_editworkspace.setToolTipText("Change workspace");
        bt_editworkspace.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_editworkspaceActionPerformed(evt);
            }
        });

        bt_ok.setText("OK");
        bt_ok.setToolTipText("Save config modify ");
        bt_ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_okActionPerformed(evt);
            }
        });

        bt_cancel.setText("Cancel");
        bt_cancel.setToolTipText("Cancel");
        bt_cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_cancelActionPerformed(evt);
            }
        });

        bt_apply.setText("Apply");
        bt_apply.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_applyActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tf_workspace, javax.swing.GroupLayout.DEFAULT_SIZE, 247, Short.MAX_VALUE)
                            .addComponent(tf_username, javax.swing.GroupLayout.DEFAULT_SIZE, 247, Short.MAX_VALUE)
                            .addComponent(tf_password, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 247, Short.MAX_VALUE)
                            .addComponent(tf_retypepassword, javax.swing.GroupLayout.DEFAULT_SIZE, 247, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(bt_editpass, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(bt_username, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(bt_editworkspace, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(68, 68, 68)
                        .addComponent(bt_ok, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(bt_cancel, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(bt_apply, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(tf_username, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(bt_username, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(tf_password, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(bt_editpass, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(tf_retypepassword, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(bt_editworkspace, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel4)
                        .addComponent(tf_workspace, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bt_cancel)
                    .addComponent(bt_ok)
                    .addComponent(bt_apply))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

private void bt_usernameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_usernameActionPerformed
// TODO add your handling code here:
    tf_username.setEnabled(true);
    usernameClick = true;
}//GEN-LAST:event_bt_usernameActionPerformed

private void bt_editpassActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_editpassActionPerformed
// TODO add your handling code here:
    tf_password.setEnabled(true);
    tf_retypepassword.setEnabled(true);
    passworkClick = true;
}//GEN-LAST:event_bt_editpassActionPerformed

private void bt_editworkspaceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_editworkspaceActionPerformed
// TODO add your handling code here:    
    workspaceClick = true;
    
    JFileChooser fc = new JFileChooser();
    fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    fc.setAcceptAllFileFilterUsed(false);

    int returnVal = fc.showOpenDialog(this);

    if (returnVal == JFileChooser.APPROVE_OPTION) {            
        String newPath = fc.getSelectedFile().toString() + "\\DB-Server";
        
        tf_workspace.setText(newPath);
        tf_workspace.setToolTipText(newPath);
    }      
}//GEN-LAST:event_bt_editworkspaceActionPerformed

private void bt_okActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_okActionPerformed
// TODO add your handling code here:
    if(usernameClick){
        config.setUsername(tf_username.getText());
    }
    
    if(passworkClick){
        if(tf_password.getText().compareTo(tf_retypepassword.getText()) == 0){
            config.setPassword(tf_password.getText());
        }else{
            JOptionPane.showMessageDialog(null, "Retype password not correct! Please retype carefully!");
            tf_password.setText("");
            tf_retypepassword.setText("");
        }
    }
    
    String oldPath = config.getWorkspace();
    if(workspaceClick){
        config.setWorkspace(tf_workspace.getText()); 
    }
    
    try {
        optionBUS.saveConfig(config, path);
    } catch (ParserConfigurationException ex) {
        Logger.getLogger(Option.class.getName()).log(Level.SEVERE, null, ex);
    }
    
    if(workspaceClick){
        changeWorkSpace(oldPath, tf_workspace.getText());
    }
    
    tabpane.remove(tabpane.getSelectedIndex());
}//GEN-LAST:event_bt_okActionPerformed

private void bt_cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_cancelActionPerformed
// TODO add your handling code here:
    tabpane.remove(tabpane.getSelectedIndex());
}//GEN-LAST:event_bt_cancelActionPerformed

private void bt_applyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_applyActionPerformed
// TODO add your handling code here:
    if(usernameClick){
        config.setUsername(tf_username.getText());
    }
    
    if(passworkClick){
        if(tf_password.getText().compareTo(tf_retypepassword.getText()) == 0){
            config.setPassword(tf_password.getText());
        }else{
            JOptionPane.showMessageDialog(null, "Retype password not correct! Please retype carefully!");
            tf_password.setText("");
            tf_retypepassword.setText("");
        }
    }
    
    String oldPath = config.getWorkspace();
    if(workspaceClick){
        config.setWorkspace(tf_workspace.getText()); 
    }
    
    try {
        optionBUS.saveConfig(config, path);
    } catch (ParserConfigurationException ex) {
        Logger.getLogger(Option.class.getName()).log(Level.SEVERE, null, ex);
    }
    
    if(workspaceClick){
        changeWorkSpace(oldPath, tf_workspace.getText());
    }
    
    DBServer.path = tf_workspace.getText();
}//GEN-LAST:event_bt_applyActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bt_apply;
    private javax.swing.JButton bt_cancel;
    private javax.swing.JButton bt_editpass;
    private javax.swing.JButton bt_editworkspace;
    private javax.swing.JButton bt_ok;
    private javax.swing.JButton bt_username;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPasswordField tf_password;
    private javax.swing.JPasswordField tf_retypepassword;
    private javax.swing.JTextField tf_username;
    private javax.swing.JTextField tf_workspace;
    // End of variables declaration//GEN-END:variables
}
